<template>
    <div id="rules">
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="角色名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="filters.enname" placeholder="英文名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="queryRole">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="list" highlight-current-row v-loading="listLoading" style="width: 100%;">
            <el-table-column type="index" width="60"></el-table-column>
            <el-table-column prop="name" label="岗位名称" sortable></el-table-column>
            <el-table-column prop="enname" label="英文名称" sortable></el-table-column>
            <el-table-column label="操作" width="180">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" icon="edit"
                               @click="handleEdit(scope.$index, scope.row)"></el-button>
                    <el-button size="small" @click="handleAssignMenu(scope.$index, scope.row)">分配菜单</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentPageChange"
                           :page-size="pagination.size"
                           :total="pagination.total" style="float:right;"></el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="formRules" ref="addForm">
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="英文名称" prop="enname">
                    <el-input v-model="addForm.enname" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click="addSubmit('addForm')" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--编辑界面-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="formRules" ref="editForm">
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="英文名称" prop="enname">
                    <el-input v-model="editForm.enname" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click="editSubmit('editForm')" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--分配菜单-->
        <el-dialog title="分配菜单" v-model="menu.dialogVisible" :close-on-click-modal="false">
            <el-row class="grid-content bg-purple user-grid" style="margin:0 0 0 18px;">
                <el-col :span="24">
                    <el-tree
                            :data="menu.treeData"
                            show-checkbox
                            accordion
                            :default-checked-keys="[1]"
                            node-key="id"
                            ref="menuTreeRef"
                            highlight-current
                            :props="menu.defaultProps">
                    </el-tree>

                </el-col>
            </el-row>
            <div slot="footer" class="dialog-footer">
                <el-button @click="menu.dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="assignMenuEnter">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>


<script>
    import util from '../../common/js/util';
    import {menuTree, roleList, roleSave} from "../../api/api";

    export default {
        data() {
            return {
                menu: {
                    dialogVisible: false,
                    treeData: [],
                    defaultProps: {
                        children: "children",
                        label: "label"
                    }
                },
                filters: {
                    name: '',
                    enname: ''
                },
                pagination: {
                    total: 0,
                    page: 1,
                    size: 20
                },

                listLoading: false,
                list: [],

                addLoading: false,
                addFormVisible: false,
                addForm: {
                    id: '',
                    name: '',
                    enname: ''
                },
                editLoading: false,
                editFormVisible: false,
                editForm: {
                    id: '',
                    name: '',
                    enname: '',
                    menuList: []
                },
                assignForm: {
                    id: '',
                    menuList: []
                },
                formRules: {
                    'name': [
                        {required: true, message: "请输入角色名称", trigger: "blur"},
                        {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'},
                        {
                            validator: function (rule, value, callback) {
                                if (value !== '') {
                                    if (!util.checkChinese(value)) {
                                        callback(new Error('角色名称不是中文'));
                                    }
                                } else {
                                    callback();
                                }
                            },
                            trigger: 'blur'
                        }
                    ],
                    'enname': [
                        {required: true, message: "请输入英文名称", trigger: "blur"},
                        {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'},
                        {
                            validator: function (rule, value, callback) {
                                if (value !== '') {
                                    if (!util.checkLetter(value)) {
                                        callback(new Error('英文名称不是字母组成'));
                                    }
                                } else {
                                    callback();
                                }
                            },
                            trigger: 'blur'
                        }
                    ]
                }
            }
        },
        mounted() {
            this.queryRole();
            this.getTreeData();
        },
        methods: {
            getTreeData() {
                let para = {}, _this = this;
                menuTree(para).then(res => {
                    _this.menu.treeData = res.data || [];
                });
            },
            handleAssignMenu: function (index, row) {
                let _this = this;
                _this.menu.dialogVisible = true;
                _this.assignForm.id = row.id;
                let menuIdList = Object.assign([], row.menuIdList);
                if (menuIdList) {
                    _this.$nextTick(function () {
                        _this.$refs.menuTreeRef.setCheckedKeys(menuIdList);
                    });
                }
            },
            assignMenuEnter: function () {
                let _this = this;
                let selectedMenus = _this.$refs.menuTreeRef.getCheckedKeys();
                _this.assignForm.menuList = [];
                for (let i = 0; i < selectedMenus.length; i++) {
                    _this.assignForm.menuList.push({id: selectedMenus[i]});
                }

                let para = Object.assign({}, _this.assignForm);
                roleSave(para).then(res => {
                    if (res.code === 20000) {
                        _this.$message({
                            message: res.message,
                            type: "success"
                        });
                        _this.queryRole();
                        _this.menu.dialogVisible = false;
                    } else {
                        _this.$message({
                            message: res.message,
                            type: "warning"
                        });
                    }
                });

            },
            queryRole: function () {
                let _this = this;
                let para = {
                    page: _this.pagination.page,
                    size: _this.pagination.size,
                    data: {
                        name: _this.filters.name,
                        enname: _this.filters.enname
                    }
                };
                this.listLoading = true;
                roleList(para).then(res => {
                    _this.pagination.total = res.data.totalElements || 0;
                    _this.list = res.data.content || [];
                    _this.listLoading = false;
                });
            },
            handleCurrentPageChange: function (val) {
                this.pagination.page = val;
                this.queryRole();
            },
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: "",
                    enname: ""
                };
            },
            addSubmit: function (formName) {
                let _this = this;
                let para = _this.addForm;

//                _this.$refs[formName].validate((valid) => {
//                    if (valid) {
                _this.$confirm('确认提交吗？', '提示', {}).then(() => {
                    this.addLoading = true;
                    roleSave(para).then(res => {

                        _this.addLoading = false;
                        _this.addFormVisible = false;
                        if (res.code === 20000) {
                            _this.$message({message: res.message, type: 'success'});
                            _this.queryRole();
                        } else {
                            _this.$message({message: res.message, type: 'warning'});
                        }
                    })
                });
//                    } else {
//                        console.log('error')
//                        return false;
//                    }
//                });
            },
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            editSubmit: function (formName) {

                let _this = this;
                let para = _this.editForm;
//                _this.$refs[formName].validate((valid) => {
//                    if (valid) {
                _this.$confirm('确认提交吗？', '提示', {}).then(() => {

                    this.editLoading = true;
                    this.editFormVisible = true;
                    roleSave(para).then(res => {
                        _this.editLoading = false;
                        if (res.code === 20000) {
                            _this.$message({message: res.message, type: 'success'});
                            _this.queryRole();
                        } else {
                            _this.$message({message: res.message, type: 'warning'});
                        }
                    })
                })
//                    } else {
//                        return false;
//                    }
//                })

            }
        }
    }
</script>
